/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.auction.rest;

import co.yixiang.dozer.service.IGenerator;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.auction.domain.AuctionConfig;
import co.yixiang.modules.auction.domain.YxAuctionConfig;
import co.yixiang.modules.auction.service.AuctionConfigService;
import co.yixiang.modules.auction.service.dto.AuctionConfigDto;
import co.yixiang.modules.auction.service.dto.AuctionConfigQueryCriteria;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
* @author ycl
* @date 2021-02-18
*/
@AllArgsConstructor
@Api(tags = "拍卖配置管理")
@RestController
@RequestMapping("/api/AuctionConfig")
public class AuctionConfigController {

    private final AuctionConfigService AuctionConfigService;
    private final IGenerator generator;


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('admin','AuctionConfig:list')")
    public void download(HttpServletResponse response, AuctionConfigQueryCriteria criteria) throws IOException {
        AuctionConfigService.download(generator.convert(AuctionConfigService.queryAll(criteria), AuctionConfigDto.class), response);
    }

    @GetMapping
    @Log("查询拍卖配置")
    @ApiOperation("查询拍卖配置")
//    @PreAuthorize("@el.check('admin','AuctionConfig:list')")
    public ResponseEntity<Object> getAuctionConfigs(AuctionConfigQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(AuctionConfigService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增拍卖配置")
    @ApiOperation("新增拍卖配置")
//    @PreAuthorize("@el.check('admin','AuctionConfig:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody AuctionConfig resources){
        return new ResponseEntity<>(AuctionConfigService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改拍卖配置")
    @ApiOperation("修改拍卖配置")
//    @PreAuthorize("@el.check('admin','AuctionConfig:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody AuctionConfig resources){
        AuctionConfigService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除拍卖配置")
    @ApiOperation("删除拍卖配置")
//    @PreAuthorize("@el.check('admin','AuctionConfig:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Long[] ids) {
        Arrays.asList(ids).forEach(id->{
            AuctionConfig auctionConfig = AuctionConfigService.getById(id);
            auctionConfig.setIsDel(1);
            AuctionConfigService.updateById(auctionConfig);
        });
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("获取拍卖时间配置")
    @ApiOperation("获取拍卖时间配置")
    @GetMapping("/getAuctionTime")
    public ResponseEntity getAuctionTime() {
        List<AuctionConfig> list = AuctionConfigService.list(new QueryWrapper<AuctionConfig>().lambda()
                .eq(AuctionConfig::getIsDel, 0));
        return new ResponseEntity<>(list ,  HttpStatus.OK);
    }
}
